package com.example.pluggabledemo;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.pluggabledemo.ams.AMSHookHelper;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import dalvik.system.DexClassLoader;

import java.io.File;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author zy
 * @version V1.0
 * @description: TODO
 * @date 2020/10/19 16:04
 */
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "debuga";

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        ThreadFactory namedThreadFactory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setName("pool-%d");
                return thread;
            }
        };
        ExecutorService singleThreadPool = new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
        singleThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                //创建一个属于我们自己插件的ClassLoader，我们分析过只能使用DexClassLoader
                String cachePath = MainActivity.this.getCacheDir().getAbsolutePath();
                String apkPath = Constants.APK_PATH;

                DexClassLoader mClassLoader = new DexClassLoader(apkPath, cachePath, cachePath, getClassLoader());
                DexHookHelper.inject(mClassLoader);
                try {
                    AMSHookHelper.hookActivityManagerNative();
                    AMSHookHelper.hookActivityThreadHandler();
                } catch (Exception e) {
                    Log.e(TAG, "加载异常了 = " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "加载异常", Toast.LENGTH_SHORT).show();
                        }
                    });
                    e.printStackTrace();
                }
            }
        });
        singleThreadPool.shutdown();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, Constants.APK_PATH);
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
            XXPermissions.with(this)
                    .permission(Permission.WRITE_EXTERNAL_STORAGE)
                    .request(new OnPermissionCallback() {
                        @Override
                        public void onGranted(List<String> permissions, boolean all) {

                        }

                        @Override
                        public void onDenied(List<String> permissions, boolean never) {
                            finish();
                        }
                    });
        }

        File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        for (File file : dir.listFiles()) {
            Log.d(TAG, "文件: " + file.getAbsolutePath());
        }

    }


    public void gotoPluginActivity(View view) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.example.plugin", "com.example.plugin.ButtonActivity"));
        startActivity(intent);
    }
}
